package us.gaaoc.framework.webapp.backingbeans.admin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.component.UISelectMany;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import javax.faces.event.ValueChangeListener;
import javax.faces.model.SelectItem;

import org.jfree.util.Log;


import us.gaaoc.framework.model.DocPageRange;
import us.gaaoc.framework.model.DocumentInstance;
import us.gaaoc.framework.model.DocumentPackage;
import us.gaaoc.framework.model.Organizations;
import us.gaaoc.framework.webapp.backingbeans.BaseBackingBean;

@SuppressWarnings("all")
public class DocumentCrud extends BaseBackingBean {

	private String uuid;
	private String name;

	private boolean uuidExists;

	private List<DocumentInstance> listOfDocInstances;
	private List<UISelectMany> selectOneList;

	private List<UIInput> startPageList;

	private List<UIInput> endPageList;

	private List<SelectItem> documentInstances;

	private List<Integer> startPageValuesList;
	private List<Integer> endPageValuesList;
	private List<String> documentValuesList;

	private String courtUuid;

	public DocumentCrud() {
		name = "";

		courtUuid = (String) FacesContext.getCurrentInstance()
				.getExternalContext().getSessionMap().get("courtUuid");

		selectOneList = new ArrayList<UISelectMany>();

		startPageList = new ArrayList<UIInput>();

		endPageList = new ArrayList<UIInput>();

		documentInstances = new ArrayList<SelectItem>();

		startPageValuesList = new ArrayList<Integer>();
		endPageValuesList = new ArrayList<Integer>();
		documentValuesList = new ArrayList<String>();

		HashMap map = (HashMap) FacesContext.getCurrentInstance()
				.getExternalContext().getSessionMap().get(
						"documentPackageValueMap");

		if (map == null) {
			listOfDocInstances = this.getModelFacade().findAllDocumentsByCourt(
					courtUuid);

			selectOneList.add(new UISelectMany());
			startPageList.add(new UIInput());
			endPageList.add(new UIInput());

			for (DocumentInstance doc : listOfDocInstances) {
				SelectItem item = new SelectItem(String.valueOf(doc.getCode()),
						doc.getDescription());
				documentInstances.add(item);
			}
		} else if (!map.isEmpty() && map != null) {

			name = (String) map.get("name");
			uuid = (String) map.get("uuid");
			uuidExists = (Boolean) map.get("uuidExists");
			listOfDocInstances = (List<DocumentInstance>) map
					.get("listOfDocInstances");
			selectOneList = (List<UISelectMany>) map.get("selectOneList");
			startPageList = (List<UIInput>) map.get("startPageList");
			endPageList = (List<UIInput>) map.get("endPageList");
			documentInstances = (List<SelectItem>) map.get("documentInstances");

			startPageValuesList = (List<Integer>) map
					.get("startPageValuesList");
			endPageValuesList = (List<Integer>) map.get("endPageValuesList");
			documentValuesList = (List<String>) map.get("documentValuesList");

			FacesContext.getCurrentInstance().getExternalContext()
					.getSessionMap().remove("documentPackageValueMap");
		}
	}

	public void startPageValueChange(ValueChangeEvent event) {
		// System.out.println("event stuff: " + event.getNewValue());
		String s = (String) event.getNewValue();

		if (!s.isEmpty()) {
			startPageValuesList.add(Integer.parseInt(s));
		}
	}

	public void endPageValueChange(ValueChangeEvent event) {
		// System.out.println("event stuff: " + event.getNewValue());
		String s = (String) event.getNewValue();

		if (!s.isEmpty()) {
			endPageValuesList.add(Integer.parseInt(s));
		}
	}

	public void documentValueChange(ValueChangeEvent event) {
		// System.out.println("event stuff: " + event.getNewValue());
		String s = (String) event.getNewValue();

		if (!s.isEmpty()) {
			documentValuesList.add(s);
		}
	}

	public String addDocument() {
		selectOneList.add(new UISelectMany());
		startPageList.add(new UIInput());
		endPageList.add(new UIInput());

		startPageValuesList.clear();
		endPageValuesList.clear();
		documentValuesList.clear();

		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("name", name);
		map.put("uuid", uuid);
		map.put("uuidExists", uuidExists);
		map.put("listOfDocInstances", listOfDocInstances);
		map.put("selectOneList", selectOneList);
		map.put("startPageList", startPageList);
		map.put("endPageList", endPageList);
		map.put("documentInstances", documentInstances);
		map.put("startPageValuesList", startPageValuesList);
		map.put("endPageValuesList", endPageValuesList);
		map.put("documentValuesList", documentValuesList);

		FacesContext.getCurrentInstance().getExternalContext().getSessionMap()
				.put("documentPackageValueMap", map);

		return null;
	}

	public String addPackage() {
		uuid = UUID.randomUUID().toString();

		DocumentPackage doc = new DocumentPackage();
		doc.setUuid(uuid);
		doc.setPackageName(name);
		doc.setCourt(this.getModelFacade().findCourtByUuid(courtUuid));

		/*
		 * System.out.println("submitted value: " + ((String[])
		 * courtList.getValue()).length); System.out.println("submitted value: "
		 * + ((String[]) courtList.getValue())[0]);
		 * 
		 * doc.setCourt(this.getModelFacade().findCourt( ((String[])
		 * courtList.getValue())[0]));
		 * 
		 * for (String s : documentValuesList) {
		 * System.out.println("doc value: " + s); }
		 * 
		 * for (Integer i : startPageValuesList) {
		 * System.out.println("start value: " + i); }
		 * 
		 * for (Integer i : endPageValuesList) {
		 * System.out.println("end value: " + i); }
		 */

		ArrayList<DocPageRange> pageRanges = new ArrayList<DocPageRange>();

		for (int i = 0; i < documentValuesList.size(); i++) {
			DocPageRange pageRange = new DocPageRange();
			pageRange.setDocPackage(doc);
			pageRange.setPageRangeEnd(endPageValuesList.get(i));
			pageRange.setPageRangeStart(startPageValuesList.get(i));
			pageRange.setUuid(UUID.randomUUID().toString());
			pageRange.setDocumentInstance(this.getModelFacade()
					.findDocumentInstanceByCode(
							Integer.parseInt(documentValuesList.get(i))));

			pageRanges.add(pageRange);
		}

		this.getModelFacade().insertDocumentPackage(doc, pageRanges);

		printSql(doc, pageRanges);

		return null;
	}

	private void printSql(DocumentPackage doc,
			ArrayList<DocPageRange> pageRanges) {

		System.out.println("INSERT INTO documentPackage VALUES ('" + doc.getUuid()
				+ "', '" + doc.getPackageName() + "', '"
				+ doc.getCourt().getUuid() + "');");

		for (DocPageRange d : pageRanges) {
			System.out.println("INSERT INTO docPageRange VALUES ('" + d.getUuid() + "', "
					+ d.getPageRangeStart() + ", " + d.getPageRangeEnd() + ", "
					+ d.getDocumentInstance().getCode() + ", '"
					+ d.getDocPackage().getUuid() + "'); /* "
					+ d.getDocPackage().getPackageName() + " */");
		}
	}

	public String back() {
		return "adminConsole";
	}

	public String updatePackage() {
		return "";
	}

	public String deletePackage() {
		return "";
	}

	public String getUuid() {
		return uuid;
	}

	public void setUuid(String uuid) {
		this.uuid = uuid;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public List<DocumentInstance> getListOfDocInstances() {
		return listOfDocInstances;
	}

	public void setListOfDocInstances(List<DocumentInstance> listOfDocInstances) {
		this.listOfDocInstances = listOfDocInstances;
	}

	public List<UISelectMany> getSelectOneList() {
		return selectOneList;
	}

	public void setSelectOneList(List<UISelectMany> selectOneList) {
		this.selectOneList = selectOneList;
	}

	public boolean isUuidExists() {
		return uuidExists;
	}

	public void setUuidExists(boolean uuidExists) {
		this.uuidExists = uuidExists;
	}

	public List<UIInput> getStartPageList() {
		return startPageList;
	}

	public void setStartPageList(List<UIInput> startPageList) {
		this.startPageList = startPageList;
	}

	public List<UIInput> getEndPageList() {
		return endPageList;
	}

	public void setEndPageList(List<UIInput> endPageList) {
		this.endPageList = endPageList;
	}

	public List<SelectItem> getDocumentInstances() {
		return documentInstances;
	}

	public void setDocumentInstances(List<SelectItem> documentInstances) {
		this.documentInstances = documentInstances;
	}
}
